<?php
// @formatter:off
/**
 * @file WCMassiveInstaller.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-06-05 00:31:35 +0000 (Wed, 05 Jun 2013) $
 *
 * $Id: WCMassiveInstaller.php 69 2013-06-05 00:31:35Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0-BETA1/includes/db/WCMassiveInstaller.php $
 */
// @formatter:on

if(!$wcDefaults['installed']) {
	require_once "{$wcPaths["includes-directory"]}/db/WCDBTable.php";
	require_once "{$wcPaths["includes-directory"]}/db/WCDBIndex.php";
}

/**
 * @class WCMassiveInstaller
 */
class WCMassiveInstaller {
	/**
	 * @var WCMassiveInstaller
	 */
	protected static $_Instance;

	protected $_db;
	protected $_dbprefix;
	protected $_structures = array();

	protected function __construct() {
		global $wcDatabaseAccess;
		global $wcDefaults;

		$this->_db = WCConnection::Instance();
		$this->_dbprefix = $wcDatabaseAccess['prefix'];
	}
	public function __destruct() {
	}
	/**
	 * Prevent users to clone the instance.
	 */
	public function __clone() {
		trigger_error(__CLASS__.": Clone is not allowed.", E_USER_ERROR);
	}
	//
	// Public methods
	public function install() {
		global $wcDefaults;
		global $wcDatabase;

		//
		// A service must avoid any database alteration information.
		$silentDatabase = defined("__SERVICE__") || !$wcDefaults["debug"];

		//
		// Prompts on web pages require special tags like '<!--' and '-->'
		$onWeb = !defined("__SHELL__");

		if(!$wcDefaults['installed']) {
			$tables = array();
			$queries = array();

			foreach($wcDatabase["tables"] as $table) {
				$item = new WCDBTable($table);
				$tables[] = $item->name();
				if(!$item->check()) {
					foreach($item->ddlQueries() as $query) {
						$queries[] = $query;
					}
				}
			}

			foreach($wcDatabase["indexes"] as $index) {
				$item = new WCDBIndex($index);
				if(!$item->check()) {
					foreach($item->ddlQueries() as $query) {
						$queries[] = $query;
					}
				}
			}

			if($wcDatabase["aggressive"]) {
				foreach($this->removeUnknownTables($tables) as $query) {
					$queries[] = $query;
				}
			}

			if(!$silentDatabase && $onWeb && isset($queries[0])) {
				echo "<!--\n";
			}

			foreach($queries as $query) {
				if($query) {
					if(!$silentDatabase) {
						echo "Query: {$query}\n";
					}
					$this->_db->exec($query);
				}
			}

			if(!$silentDatabase && $onWeb && isset($queries[0])) {
				echo "-->\n";
			}
		}
	}
	//
	// Protected methods
	protected function removeUnknownTables(&$valids) {
		$queries = array();

		if(count($valids) > 0) {
			global $wcDatabaseAccess;

			$query = "select  table_name\n";
			$query .= "from    information_schema.tables\n";
			$query .= "where   table_name not in ('".implode("','", $valids)."')\n";
			$query .= " and    table_schema = '{$wcDatabaseAccess["database"]}'\n";
			$result = $this->_db->query($query, true);
			if($result && $result->rowCount() > 0) {
				foreach($result->fetchAll() as $row) {
					$queries[] = "drop table {$row["table_name"]} cascade";
				}
			}
		}

		return $queries;
	}
	//
	// Public class methods
	public static function Instance() {
		if(!isset(self::$_Instance)) {
			$c = __CLASS__;
			self::$_Instance = new $c();
		}

		return self::$_Instance;
	}
}
?>